package cs5226.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DBUtil {
	public static final String DEFAULT_DATASOURCE = "jdbc.cs5226";

	private static HashMap<String, DataSource> dsStore = null;

	static {
		init();
	}

	private static void init() {
		dsStore = new HashMap<String, DataSource>();
		initDatasource(DEFAULT_DATASOURCE);
	}

	public static void initDatasource(String jndiName) {
		InitialContext ic = null;

		try {
			ic = new InitialContext();
			DataSource ds = (DataSource) ic.lookup(jndiName);

			if (ds != null)
				dsStore.put(jndiName, ds);
		} catch (Throwable t) {
			System.out.println ("====================================== ERROR DURING INITIALZIATION");
			t.printStackTrace();
		} finally {
			try {
				if (ic != null)
					ic.close();
			} catch (NamingException e) {
				e.printStackTrace();
			}
		}
	}

	public static Connection getConnection() throws SQLException {
		DataSource ds = dsStore.get(DEFAULT_DATASOURCE);
		// System.out.println ("Data source " + jndiName + " found @ " + ds);
		if (ds != null)
			return ds.getConnection();
		else
			return null;
	}

	/*
	 * public static Connection getConnection() { String url
	 * ="jdbc:oracle:thin:@dbtune2.comp.nus.edu.sg"; String userid="a0032717";
	 * String pwd="cs5226ora"; Connection connection = null; try {
	 * 
	 * Class.forName("oracle.jdbc.driver.OracleDrive"); connection =
	 * DriverManager.getConnection( url, userid,pwd); } catch (Exception e) {
	 * System.out.print("====================" + e.getMessage());
	 * e.printStackTrace(); } return connection; }
	 */

	public static void closeResultSet(ResultSet result) {
		try {
			if (result != null && !result.isClosed()) {
				result.close();
			}
		} catch (Exception se) {
			se.printStackTrace();
		}
	}

	public static void closeStatement(Statement stmt) {
		try {
			if (stmt != null && !stmt.isClosed()) {
				stmt.close();
			}
		} catch (Exception se) {
			se.printStackTrace();
		}
	}

	public static void closeConnection(Connection conn) {
		try {
			if (conn != null && !conn.isClosed()) {
				conn.close();
			}
		} catch (Exception se) {
			se.printStackTrace();
		}
	}
	
	public static void close(ResultSet result, Statement stmt, Connection conn) {
		closeResultSet(result);
		closeStatement(stmt);
		closeConnection(conn);
	}

	/*
	public static void closeAll(ResultSet result) {
		if (result == null)
			return;
		try {
			Statement stmt = result.getStatement();
			Connection conn = (stmt != null) ? stmt.getConnection() : null;
			closeResultSet(result);
			closeStatement(stmt);
			closeConnection(conn);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	*/
}
